﻿namespace Forest.Lion.Domain {
    using Forest.Lion.Domain.Contracts;
    using Forest.Lion.Data;
    using Forest.Lion.Domain.Models;
    using Forest.Lion.Model;
    using System;
    public class UserServices : IUserServices {
        private readonly ICompanyRepository _companyRepository;

        public UserServices(ICompanyRepository companyRepository) {
            _companyRepository = companyRepository;
        }

        public bool ValidateUser(string userName, string password) {
            return true;
        }

        public User RegisterUser(User registerUser) {
            var company = new Company {
                Id = Guid.NewGuid(),
                UserName = registerUser.UserName,
                Password = registerUser.Password,
                PasswordSalt = registerUser.Password,
                CompanyName = registerUser.CompanyName,
                Tel = registerUser.Tel,
                Contact = registerUser.Contact,
                Mobile = registerUser.Mobile,
                LastUpdated = DateTime.Now,
                DateCreated = DateTime.Now
            };

            _companyRepository.Create(company);
            var com = _companyRepository.GetCompany(company.Id);

            return new User {
                Id = com.Id,
                CompanyName = com.CompanyName,
                Mobile = com.Mobile,
                Tel = com.Tel,
                UserName = com.UserName,
                Contact = com.Contact
            };
        }

        public User GetAuthenticatedUser(string userName) {
            var user = _companyRepository.GetCompanyByUserName(userName);

            if (user != null)
                return ToServiceUser(user);

            return null;
        }

        public bool ChangePassword(string usernameOrEmail, string newPassword, string oldPassword) {
            return true;
        }
        public void UpdateUser(User updatedUser) { }

        public bool DeleteUser(User deletedUser) {
            return true;
        }

        public User ToServiceUser(Model.Company dataUser) {
            User user = new User() {
                Id = dataUser.Id,
                CompanyName = dataUser.CompanyName,
                Contact = dataUser.Contact,
                Mobile = dataUser.Mobile,
                Tel = dataUser.Tel,
                UserName = dataUser.UserName,
            };

            return user;
        }
    }
}
